clear all 
set more off
*set maxvar 20000

cd "C:\Users\Lester\Dropbox\June 2016\Writeup\Economic Inquiry\EI R and R"
use "C:\Users\Lester\Dropbox\data_dta\master.dta", clear

tostring id grade year, replace
gen course = grade + " " + subject + " " + cohort
gen class = year + " " + course
gen studentxclass = id + " " + class
gen gradebook = year + grade + cohort
destring id grade year, replace

drop if substr(subject, 1, 2) == "it" | substr(subject, 1, 2) == "cs" 
		
global controls order_ass num_month bulg

gen malelate = male*late_start
gen maleteachermale = male*teacher_male

*****************************************************************   
/*						PANEL A	      				   */	   
*****************************************************************
eststo clear

preserve
keep if early==1

eststo clear

eststo: xi: qui reg Zmarks i.male, vce(cluster class)
eststo: xi: qui reg Zmarks i.male malelate maleteachermale teacher_male late_start, vce(cluster class)
eststo: xi: qui reg Zmarks i.male malelate maleteachermale teacher_male late_start i.month, vce(cluster class)
eststo: xi: qui reg Zmarks i.male malelate maleteachermale teacher_male late_start i.month $controls, vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start i.month, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start $controls, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start i.month $controls, absorb(class) vce(cluster class)

label var _Imale "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-subsamples.tex", ///
    keep(_Imale_1 maleteachermale malelate) replace se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(_Imale_1 "\underline{Early Classes}", nolabel) noconstant nogaps ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") label postfoot("")

restore

*****************************************************************   
/*						PANEL B	-	   */	   
*****************************************************************
preserve
keep if late==1

eststo clear

eststo: xi: qui reg Zmarks male, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start i.month, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start i.month $controls, vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start i.month, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start $controls, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start i.month $controls, absorb(class) vce(cluster class)

label var male "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-subsamples.tex", ///
	keep(maleteachermale malelate male) append se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps ///
	refcat(male "\hline \underline{Late Classes}", nolabel) ///
    mtitles("" "" "" "" "" "" "" "") label prehead("") postfoot("") posthead("") 
* "studentfe Student FE"
restore

*****************************************************************   
/*						PANEL C	- STEM courses		   */	   
*****************************************************************
preserve
keep if stem==1

eststo clear

eststo: xi: qui reg Zmarks male, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start i.month, vce(cluster class)
eststo: xi: qui reg Zmarks male malelate maleteachermale teacher_male late_start i.month $controls, vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start i.month, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start $controls, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks male malelate maleteachermale teacher_male late_start i.month $controls, absorb(class) vce(cluster class)

label var male "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-subsamples.tex", ///
	keep(maleteachermale malelate male) append se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps ///
	refcat(male "\hline \underline{STEM courses}", nolabel) ///
    mtitles("" "" "" "" "" "" "" "") label prehead("") postfoot("") posthead("") 
restore

*****************************************************************   
/*						PANEL D - Non-STEM courses			   */	   
*****************************************************************
preserve
keep if stem==0

eststo clear

eststo: xi: qui reg Zmarks male, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "  
eststo: xi: qui reg Zmarks male i.male*late_start i.male*i.teacher_male, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "   
eststo: xi: qui reg Zmarks male i.male*late_start i.male*i.teacher_male i.month, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe "X"
estadd local controls " "
eststo: xi: qui reg Zmarks male i.male*late_start i.male*i.teacher_male i.month $controls, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe "X"
estadd local controls "X"
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male i.month, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe "X"
estadd local controls " "
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male $controls, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls "X"
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male i.month $controls, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe "X"
estadd local controls "X"

label var male "\ \ \ Male"
label var _ImalXtea "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var _ImalXlate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-subsamples.tex", ///
	keep( _ImalXtea_1_1 _ImalXlate__1 male) append se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps ///
	refcat(male "\hline \underline{Non-STEM courses}", nolabel) ///
	scalars("classfe Class FE" "monthfe Month FE" "controls Controls") ///
    mtitles("" "" "" "" "" "" "" "") label prehead("") posthead("") 
restore
